<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-stock__birth}">
      <search-bar ref="SearchBar" model="birth" :fields="searchFields"  @search="search"></search-bar>
      <multi-table :data="m_ltData" :model="m_stModel"></multi-table>
      <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%;">
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <el-table-column v-for="field in m_ltFields" :key="field.prop" :prop="field.prop" :fixed="field.fixed" :label="field.label" header-align="center" align="center"></el-table-column>
        <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
          <template slot-scope="scope">
            <el-button v-if="$hasPermission('stock:birth:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">{{ $t('update') }}</el-button>
            <el-button v-if="$hasPermission('stock:birth:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t('delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-model v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList" model="birth" :fields="m_ltFields"></add-model>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from '../birth-add-or-update'
import AddModal from '../pig/add-or-update'
import SearchBar from '../search-bar'

export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/stock/birth/page',
        getDataListIsPage: true,
        exportURL: '/stock/birth/export',
        deleteURL: '/stock/birth',
        deleteIsBatch: true
      },
      m_ltFields: [],
      m_ltData: [],
      m_stModel: {},
      dataForm: {
        id: ''
      }
    }
  },
  components: {
    AddOrUpdate,
    'add-model': AddModal,
    SearchBar
  },
  mounted () {
    this.getFields()
  },
  methods: {
    getFields () {
      let url = '/report/gestation/properties'
      this.$http.get(
        url,
        {
          params: {
            order: this.order,
            orderField: this.orderField,
            page: this.mixinViewModuleOptions.getDataListIsPage ? this.page : null,
            limit: this.mixinViewModuleOptions.getDataListIsPage ? this.limit : null,
            ...this.dataForm
          }
        }
      ).then(({ data: res }) => {
        this.dataListLoading = false
        if (res.code !== 0) {
          this.dataList = []
          this.total = 0
          return this.$message.error(res.msg)
        }
        this.dataList = this.mixinViewModuleOptions.getDataListIsPage ? res.data.list : res.data
        this.total = this.mixinViewModuleOptions.getDataListIsPage ? res.data.total : 0
      }).catch(() => {
        this.dataListLoading = false
      })
    },
    onAdd () {
      this.$http.get(
        'report/page',
        {
          params: {
            page: 1,
            limit: 10
          }
        }
      ).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        console.log(res)
        this.m_ltData = res.data.list
      }).catch(() => {
        this.dataListLoading = false
      })
    }
  }
}
</script>
